package com.demo.HashSet_;

import java.util.HashSet;
import java.util.Objects;

/**
 * @author gy
 * @date 2022/4/9
 */
public class HashSet01 {


    public static void main(String[] args) {
        HashSet<Object> objects = new HashSet<>();
        objects.add(new Employee(new MyDate(2022,4,9),"张三",18));
        objects.add(new Employee(new MyDate(2022,4,9),"张三",80));
        System.out.println("hashSet:"+ objects);
    }

}
class Employee{
    private MyDate myDate;
    private String name;
    private int age;

    public Employee(MyDate myDate, String name, int age) {
        this.myDate = myDate;
        this.name = name;
        this.age = age;
    }

    public String getName() {
        return name;
    }

    public void setName(String name) {
        this.name = name;
    }

    public int getAge() {
        return age;
    }

    public void setAge(int age) {
        this.age = age;
    }

    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        Employee employee = (Employee) o;
        return Objects.equals(myDate, employee.myDate) &&
                Objects.equals(name, employee.name);
    }

    @Override
    public int hashCode() {
        return Objects.hash(myDate, name);
    }

    @Override
    public String toString() {
        return "Employee{" +
                "myDate=" + myDate +
                ", name='" + name + '\'' +
                ", age=" + age +
                '}';
    }
}
class MyDate{
    private int year;
    private int month;
    private int day;

    public MyDate(int year, int month, int day) {
        this.year = year;
        this.month = month;
        this.day = day;
    }

    public int getYear() {
        return year;
    }

    public void setYear(int year) {
        this.year = year;
    }

    public int getMonth() {
        return month;
    }

    public void setMonth(int month) {
        this.month = month;
    }

    public int getDay() {
        return day;
    }

    public void setDay(int day) {
        this.day = day;
    }

//    此处的hashcode 和 equals值 直接影响到了 员工对象的 hashcode 和 equals值
    @Override
    public boolean equals(Object o) {
        if (this == o) return true;
        if (o == null || getClass() != o.getClass()) return false;
        MyDate myDate = (MyDate) o;
        return year == myDate.year &&
                month == myDate.month &&
                day == myDate.day;
    }

    @Override
    public int hashCode() {
        return Objects.hash(year, month, day);
    }


    @Override
    public String toString() {
        return "{"+year+ "年"+month+ "月"+day+ "日}";
    }
}